//===--------------------- InstallSync.cpp ---------------------------------===//
//
//                          InstallSync
//
//		This program is licensed under GNU GPL v3 see LICENSE.TXT for details.
//
// Author(s): Andrew Weaver ( aweaver1989@gmail.com )
//              Aaron Landwehr ( snaphat@gmail.com )
//
// (c) 2011 
//
//===----------------------------------------------------------------------===//
//
//===----------------------------------------------------------------------===//


#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <string>
#include <dirent.h>
#include <curl/curl.h>
#include <md5.h>
#include <fstream>
#define BUFFER_SIZE 1024
#define BASE_URL "http://www.filehippo.com/download_"
std::string excludedDirs[] = {"D://Dropbox//Public//Installers//IM Clients" };
int settings(char* wdir)
{
	std::string wd = wdir;
	
	int temp =  wd.find_last_of("//");
	std::string settingsDir = settingsDir = settingsDir.substr(0 ,temp - 1);
	std::cout << settingsDir;
	//Excluded Directories
	
	


	//std::ifstream infile;
	//infile.open(, std::ifstream::in | std::ifstream::binary);

	return 0;
}

int getdir(const char* dir)
{
	DIR *dp = opendir(dir);
	struct dirent* dirp;
	if(dp == NULL)
	{
		std::cout << "That is not a valid directory";	
	}
	else
	{
		while ( (dirp = readdir(dp)) != NULL)
		{
			if	( dirp->d_type == DT_DIR && dirp->d_name != excludedDirs[1])
			{
				if (strcmp(dirp->d_name,".") != 0 && strcmp(dirp->d_name,"..") != 0 )
				{
					std::string filename = dirp->d_name;
					std::string fulldir = dir;

					fulldir += "\\" + filename;

					getdir(fulldir.c_str());
					std::cout << fulldir << std::endl;
				}
			}
		}

	}
	closedir(dp);
	return 0;
}


std::string setdir()
{
	std::string dir;
	std::cout << "Please Enter the desired directory: ";
	std::cin >> dir;

	return dir;
}
std::string buffer;
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *userdata)
{
	buffer += (char*)ptr;
	return size*nmemb;
}

int getURL()
{
	CURL *curl;
    CURLcode res;
	curl = curl_easy_init();
    if(curl) 
	{
		curl_easy_setopt(curl, CURLOPT_URL, "http://www.filehippo.com/download_google_chrome/tech/");
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
		//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
		res = curl_easy_perform(curl);
		
		
		/* always cleanup */ 
		curl_easy_cleanup(curl);
	}
	std::cout << "URL Information Fetched" << std::endl;
	return 0;
}

std::string getHash(std::string fullpath)
{
		
		
		//DIR *dp = opendir(dir);
		//struct dirent* dirp;
		md5_state_t state;
		md5_byte_t digest[16];
		char hex_output[16*2 + 1];
		std::string localHash;

	std::ifstream infile;
	infile.open(fullpath, std::ifstream::in | std::ifstream::binary);
	if (infile.is_open())
    {
		//initialize the md5 summer.
		md5_init(&state);
		char buffer[BUFFER_SIZE];

		// Grab every 255 bytes of the file and append them to the md5 summer.
		while (infile.read(buffer, BUFFER_SIZE) )
		{
			md5_append(&state, (const md5_byte_t *) buffer, infile.gcount());
		}
		// Grab the last bytes of the file and append them to the md5 summer.
		md5_append(&state, (const md5_byte_t *) buffer, infile.gcount());

		// Generate the hash code.
		md5_finish(&state, digest);

		//Get the plain-text output for the hash code.
		for (unsigned di = 0; di < 16; ++di)
		{
			sprintf(hex_output + di * 2, "%02X", digest[di]);
		}
		std::cout << hex_output;
		localHash = hex_output;
		
		infile.close();
	}
	else
	{
		std::cout << "Error opening file";
	}
	return localHash;
}

bool findHash(std::string localHash)
{
	size_t found = buffer.find(localHash,0);
	return (found != std::string::npos);
}

std::string findDownloadFile()
{   
	std::string temp = "google_chrome/download/";
	size_t pos = buffer.find(temp,0);
	
	std::string link = buffer.substr(pos + temp.length(), 32);



	std::cout<< BASE_URL + temp + link;
	return BASE_URL + temp + link;



}


int main(int argc, char* argv[])
{
	settings(argv[0]);
	std::string fullpath = "D:\\Dropbox\\Public\\Installers\\chrome_installer.exe";
	std::string dir;
	std::string localhash;
	dir = setdir();
	getdir(dir.c_str());
	localhash = getHash(fullpath);
	getURL();
	//std::cout << buffer << std::endl;
	if (findHash(localhash) == false)
	{
		//Download here
		
	}
	findDownloadFile();
	


	// stop and wait till done looking at the output
	char c;
	std::cin >> c;

	return 0;
   
}
